package com.cndw.kungfu.event.model;

import java.util.Collections;
import java.util.List;

import com.cndw.kungfu.event.BaseHandler;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.activity.domain.WorldBoos;

/**
 * 世界boss刷新脚本
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-11-16 下午7:37:48
 */
public class WorldBossHandler extends BaseHandler implements Runnable {
	private WorldBoos worldBoos;
	private int state = 0;
	@Deprecated
	public static int currentBossId = 0;

	public WorldBossHandler(WorldBoos worldBoos) {
		this.worldBoos = worldBoos;
	}

	public WorldBossHandler(WorldBoos worldBoos, int state) {
		this.worldBoos = worldBoos;
		this.state = state;
	}

	public static void runBoss() {
		try {
			List<WorldBoos> bossList = WorldBoos.getAllList();
			Collections.sort(bossList);
			int morningTime = TimeProcesser.getMorningTime();
			int currentTime = TimeProcesser.getUnixTime();
			StringBuffer sb = new StringBuffer();
			for (WorldBoos worldBoos : bossList) {
				int planTime = morningTime + worldBoos.getRefreshTime();
				if (planTime <= currentTime && planTime + WorldBoos.bossLifeTime > currentTime) {
					LogUtil.getSystem().info("boss now " + worldBoos.getId());
					Context.getTimerServer().execute(new WorldBossHandler(worldBoos));
					continue;
				} else {
					int delay = planTime - currentTime;
					delay = delay < 0 ? delay + 86400 : delay;
					Context.getTimerServer().scheduleSeconds(new WorldBossHandler(worldBoos), delay);
					
					sb.append(worldBoos.getId()).append(",").append(delay).append(";");

				}
			}
			LogUtil.getSystem().info("world boss to process:"+sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Deprecated
	public static void runBossOld() {
		try {
			int nextId = 12;
			List<WorldBoos> bossList = WorldBoos.getAllList();
			Collections.sort(bossList);
			int morningTime = TimeProcesser.getMorningTime();
			int currentTime = TimeProcesser.getUnixTime();
			for (WorldBoos worldBoos : bossList) {
				int planTime = morningTime + worldBoos.getRefreshTime();
				if (planTime > currentTime) {
					nextId = worldBoos.getId();
					break;
				} else if (planTime <= currentTime && planTime + WorldBoos.bossLifeTime > currentTime) {
					log.info("boss now " + worldBoos.getId());
					Context.getTimerServer().execute(new WorldBossHandler(worldBoos));
					continue;
				}

			}
			WorldBoos worldBoos = WorldBoos.getById(nextId);
			int delay = morningTime + worldBoos.getRefreshTime() - TimeProcesser.getUnixTime();
			delay = delay < 0 ? delay + 86400 : delay;
			Context.getTimerServer().scheduleSeconds(new WorldBossHandler(worldBoos), delay);

			currentBossId = worldBoos.getId();

			log.info("world boss refresh next id:" + worldBoos.getId() + ":delay:" + delay);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void run() {
		try {
			if (state == 0) {
				worldBoos.Refresh();
				int nextTime = worldBoos.getLastRefresh() + WorldBoos.bossLifeTime - TimeProcesser.getUnixTime();
				Context.getTimerServer().scheduleSeconds(this, nextTime);
				int nextDelay = worldBoos.getLastRefresh() + 24 * 3600 - TimeProcesser.getUnixTime();
				Context.getTimerServer().scheduleSeconds(new WorldBossHandler(worldBoos), nextDelay);
				LogUtil.getSystem().info("world boss to process:" + worldBoos.getId() + "|proscess delay :" + nextTime+"|next:"+nextDelay);

			} else if (state == 1) {
				worldBoos.process();
				LogUtil.getSystem().info("world boss do process:" + worldBoos.getId());
			}
			state++;

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
